// src/config/buildStaticEnvConfig.ts
function buildStaticEnvConfig(portalID, hublet, hsEnv, hsDeployed, hsJSRGates, hsRenderSource) {
  const getPortalID = () => portalID;
  const getHubID = getPortalID;
  const getHublet = () => hublet;
  const getInternalHSEnv = () => hsEnv;
  const getIsDeployed = () => hsDeployed;
  const getInternalRenderSource = () => hsRenderSource;
  const getGates = () => hsJSRGates;
  const toJSON = () => ({
    portalID,
    hublet,
    hsEnv,
    hsDeployed,
    hsJSRGates
  });
  const equals = (envConfig) => {
    return portalID === envConfig.getPortalID() && hsEnv === envConfig.getInternalHSEnv() && hublet === envConfig.getHublet() && hsDeployed === envConfig.getIsDeployed();
  };
  return {
    getPortalID,
    getHubID,
    getHublet,
    getInternalHSEnv,
    getIsDeployed,
    equals,
    getGates,
    getInternalRenderSource,
    toJSON
  };
}

// src/config/singleton.ts
var hsConfigKey = typeof Symbol !== "undefined" ? Symbol.for("hs-config-singleton") : "HS_CONFIG_SINGLETON";
function initConfigSingleton(portalID, hublet, hsEnv, hsDeployed, hsJSRGates) {
  if (import.meta.env.SSR) {
    throw new Error(
      "Config singletons only work in the browser, not server-side"
    );
  }
  const envConfig = buildStaticEnvConfig(
    portalID,
    hublet,
    hsEnv,
    hsDeployed,
    hsJSRGates
  );
  if (window[hsConfigKey]) {
    const existingEnvConfig = window[hsConfigKey];
    if (!envConfig.equals(existingEnvConfig)) {
      throw new Error(
        `Mismatch when setting environment config ${JSON.stringify(
          envConfig.toJSON()
        )}, existing: ${JSON.stringify(existingEnvConfig.toJSON())}`
      );
    } else {
      return;
    }
  }
  window[hsConfigKey] = envConfig;
}
function internalGetConfigSingletonForTest() {
  return window[hsConfigKey];
}
function internalUpdateConfigSingletonForTest(environmentConfig) {
  window[hsConfigKey] = environmentConfig;
}
function initConfigSingletonFromJSON({
  portalID,
  hublet,
  hsEnv,
  hsDeployed,
  hsJSRGates
}) {
  return initConfigSingleton(portalID, hublet, hsEnv, hsDeployed, hsJSRGates);
}
function assertConfigSingletonDefined() {
  if (!window[hsConfigKey]) {
    throw new Error("No environment config singleton has been setup");
  }
}
function getConfigSingleton() {
  if (import.meta.env.SSR) {
    throw new Error(
      "Config singletons only work in the browser, not server-side"
    );
  }
  assertConfigSingletonDefined();
  return window[hsConfigKey];
}

export {
  buildStaticEnvConfig,
  initConfigSingleton,
  internalGetConfigSingletonForTest,
  internalUpdateConfigSingletonForTest,
  initConfigSingletonFromJSON,
  getConfigSingleton
};
